﻿using AutoMapper;
using HuiLian.Common.Attributes;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Common;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.Dma.Input;
using HuiLian.Service.Platform.Dma.Output;
using HuiLian.Service.Platform.Dma;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HuiLian.Repository.Platform.Dma;

namespace HuiLian.Service.Platform.Dma
{
    public class DmaService : IDmaService
    {
        private readonly IMapper _mapper;
        private readonly IDmaRepository _dmaRepository;
        public DmaService(IMapper mapper, IDmaRepository moduleRepository)
        {
            _dmaRepository = moduleRepository;
            _mapper = mapper;
        }

        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _dmaRepository.GetAsync<DmaGetOutput>(id);
            return ResponseOutput.Ok(result);
        }

        public async Task<IResponseOutput> ListAsync(string key)
        {
            var data = await _dmaRepository
                .Select
                .WhereIf(key.NotNull(), a => a.名称.Contains(key) || a.运行年份.Contains(key))
                //.OrderBy(a => a.ParentId)
                //.OrderBy(a => a.Sort)
                .OrderBy(true, c => c.运行年份)
                .ToListAsync<DmaListOutput>();

            return ResponseOutput.Ok(data);
        }

        public async Task<IResponseOutput> PageAsync(PageInput<DmaEntity> input)
        {
            //var key = input.Filter?.Label;

            long total;
            var list = await _dmaRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            .Count(out total)
            .OrderBy(true, c => c.运行年份)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync();

            var data = new PageOutput<DmaEntity>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        public async Task<IResponseOutput> AddAsync(DmaAddInput input)
        {
            var entity = _mapper.Map<DmaEntity>(input);
            var id = (await _dmaRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        public async Task<IResponseOutput> UpdateAsync(DmaUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _dmaRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("视图不存在！");
            }

            _mapper.Map(input, entity);
            await _dmaRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _dmaRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _dmaRepository.DeleteAsync(id) > 0;

            return ResponseOutput.Result(result);
        }

        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                foreach (var id in ids)
                {
                    var result = await _dmaRepository.DeleteAsync(id);
                }
                return ResponseOutput.Result(true);
            }
            else
            {
                return ResponseOutput.Result(false);
            }
        }
    }
}
